﻿using App.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YTCMS.Core;
using YTCMS.Domain.Entities;
using YTCMS.Services.Services;

namespace App.Controllers
{
    public class ProductsController : BaseController
    {
        private readonly IproductsService _products;
        private readonly IcategoryService _category;
        private readonly IimagesService _images;
        public ProductsController(IproductsService products, IcategoryService category, IimagesService images)
        {
            this._products = products;
            this._category = category;
            this._images = images;
            
        }
        //
        // GET: /Products/
        public ActionResult Index(string keyword,string orderby, int?categoryid, int? cid=0, int? pageIndex = 1, int? pageSize = 16)
        {
            var model = _products.GetMany(s => s.State == (int)CommonState.正常, Order.desc, "Sort");
            int pcount = 0;
            model = QueryHelper.WhereIf<products>(model, s => s.Name.Contains(keyword), !string.IsNullOrWhiteSpace(keyword));
            model = QueryHelper.WhereIf<products>(model, s => s.CategoryId == categoryid, categoryid != null);
            model = _products.PageList(model, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            ViewData["keyword"] = keyword;
            if (categoryid != null)
            {
                category c =  _category.Get(s => s.Id == categoryid);
                ViewData["keyword"] = c.Name;
            }
            return View(model);
        }

        public ActionResult Search(string keyword, string orderby, int? categoryid, int? cid = 0, int? pageIndex = 1, int? pageSize = 16)
        {
            var model = _products.GetMany(s => s.State == (int)CommonState.正常, Order.desc, "Sort");
            int pcount = 0;
            model = QueryHelper.WhereIf<products>(model, s => s.Name.Contains(keyword), !string.IsNullOrWhiteSpace(keyword));
            model = QueryHelper.WhereIf<products>(model, s => s.CategoryId == categoryid, categoryid != null);
            model = _products.PageList(model, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            return PartialView("List", model);
        }



        public ActionResult Details(int id)
        {
            var model = _products.Get(s => s.Id == id);
            ViewData["imgs"] = _images.GetMany(s => s.Pid == id && s.Type == (int)Images.线路);
            return View(model);
        }
	}
}